package com.szy.assembly;

import java.util.HashMap;

public class Timer extends Peripheral{

    private final int offset_en=0;
    private final int offset_div=0x01;
    private final int offset_ov=0x02;

    private final int offset_cnt=0x03;
    private final int offset_int_en=0x100;


    public Timer(int baseAddress) {
        super(baseAddress);
        registers=new HashMap<>();
        registers.put(base+offset_en,0);
        registers.put(base+offset_div,0);
        registers.put(base+offset_int_en,0);
        registers.put(base+offset_ov,0);
        registers.put(base+offset_cnt,0);
    }

//    @Override
//    public boolean setRegister(int address, int value) {
//        if(!super.setRegister(address, value)){
//            return false;
//        }
//        if(address == base + offset_en){
//
//        }
//
//        return true;
//    }

    @Override
    public void tickTask() {
        if(registers.get(base + offset_en) != 0){
            int cnt=registers.get(base+offset_cnt);
            cnt++;
            if(cnt >= registers.get(base+offset_ov)){
                cnt=0;
                if(registers.get(base+offset_int_en) != 0){
                    interruptRequest.interruptHandle(InterruptVector.InterruptName.TIMER_OVERFLOW_0);
                }
            }
            registers.put(base+offset_cnt,cnt);

        }
    }
}
